//
//  DatabaseMigrateStep2.m
//  Apps
//
//  Created by Kevin Chen on 2019/8/7.
//  Copyright © 2019 appscomm. All rights reserved.
//

#import "DatabaseMigrateStep2.h"

@implementation DatabaseMigrateStep2
//@synthesize stepProgress = _stepProgress;

//- (instancetype)init
//{
//    self = [super init];
//    if (self) {
//        _stepProgress = [NSProgress progressWithTotalUnitCount:1];
//    }
//    return self;
//}

- (void)goUpActionWithCurrentDBVersion:(NSInteger)version progressHandler:(void (^)(NSProgress * _Nonnull))progressHandler error:(NSError * _Nullable __autoreleasing *)error {
    
    NSProgress *pro = [NSProgress progressWithTotalUnitCount:1];
    
    __block NSProgress *customMigratePro = nil;
    
    if (version == self.completeVersion - 1) {
        LYDataBaseMigrationAction *action = [[LYDataBaseMigrationAction alloc] initWithDatabase:[LYDatabase database]];
        [action customRenewMigrateTableWithClass:[Sport class] pageSize:100 dropOldTableWhenNoError:YES handler:^NSArray * _Nullable(NSInteger page, NSArray<NSDictionary *> * _Nonnull originData) {
            
            NSArray <Sport *>*sportArr = [Sport convertDictionaryArrayToObjectArrayWithDictionaryArray:originData];
            return sportArr;

        } errorCapture:^(NSError * _Nonnull error, BOOL * _Nonnull needStop) {
            if (error) {
                NSLog(@"%@",error);
            }
        } progress:^(NSProgress * _Nonnull completeProgress) {
            NSLog(@"%@",completeProgress.localizedDescription);
            if (customMigratePro != completeProgress) {
                customMigratePro = completeProgress;
                [pro addChild:customMigratePro withPendingUnitCount:1];
            }
            if (progressHandler) {
                progressHandler(pro);
            }
            NSLog(@"pro = %@",completeProgress.localizedDescription);

        } completion:^{
            NSLog(@"Cmp");
        }];
        
//        [action customRenewMigrateTableWithClass:[Sport class] pageSize:100 dropOldTable:NO handler:^NSArray * _Nullable(NSInteger page, NSArray<NSDictionary *> * _Nonnull originData) {
//
//            NSArray <Sport *>*sportArr = [Sport convertDictionaryArrayToObjectArrayWithDictionaryArray:originData];
//
//            return sportArr;
//
//        } completion:^{
//            NSLog(@"Cmp");
//        }];
    }
    
}

- (void)goDownActionWithError:(NSError * _Nullable __autoreleasing *)error {
    
}

- (NSInteger)completeVersion {
    return 2;
}
@end
